/*
 * Copyright 2020 Mike Penz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.iconics.animation;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;

import com.mikepenz.iconics.IconicsBrush;

/**
 * @author pa.gulko zTrap (30.11.2018)
 */
public class BlinkAlphaProcessor extends IconicsAnimationProcessor {
    private static final long DEFAULT_DURATION = 500L;
    private static final int MAX_ALPHA = 255;
    private int minimumAlpha = 0;
    private int maximumAlpha = MAX_ALPHA;

    /**
     * 默认构造
     */
    public BlinkAlphaProcessor() {
        setDuration(DEFAULT_DURATION);
        setAnimationTag("blink_alpha");
        setRepeatMode(ValueAnimator.RepeatMode.REVERSE);
    }

    /**
     * 设置最小透明度
     *
     * @param minimumAlpha 最小透明度
     */
    public void setMinimumAlpha(int minimumAlpha) {
        if (minimumAlpha >= 0 && minimumAlpha <= MAX_ALPHA) {
            this.minimumAlpha = minimumAlpha;
        }
    }

    /**
     * 设置最大透明度
     *
     * @param maximumAlpha 最大透明度
     */
    public void setMaximumAlpha(int maximumAlpha) {
        if (maximumAlpha >= 0 && maximumAlpha <= MAX_ALPHA) {
            this.maximumAlpha = maximumAlpha;
        }
    }

    @Override
    public void processorPreDraw(Canvas canvas, IconicsBrush<Paint> iconBrush, IconicsBrush<Paint> iconContourBrush,
        IconicsBrush<Paint> backgroundBrush, IconicsBrush<Paint> backgroundContourBrush) {
        final int max = 100;
        int alphaByPercent = (maximumAlpha - minimumAlpha) / max;
        int alpha = (int) (getAnimatedPercent() * alphaByPercent);
        iconBrush.setAlpha(alpha);
        iconContourBrush.setAlpha(alpha);
        backgroundBrush.setAlpha(alpha);
        backgroundContourBrush.setAlpha(alpha);
    }

    @Override
    public void processPostDraw(Canvas canvas) {
    }
}
